﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CategoryManager.h"  
#include "IDocument.h"
#include "ICategory.h"
#include "ICategoryLibrary.h"
#include "CategoryDefine.h"
#include "IGraphicsStyleManager.h"
#include "IGraphicsMaterialManager.h"
#include "IGraphicsStyleData.h"
#include "IGraphicsMaterialData.h"
#include "IGraphicsMaterial.h"
#include "IUserTransaction.h"

#include "IGraphicsStyle.h"
#include "IElementBasicInformation.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace Sample;
using namespace gcmp;


void CategoryManager::Initialize(IDocument* pDocument)
{
    //类别定义方式：
    //1、接口 2、配置文件(Data\category_and_style_config\Categories.xml)
    OwnerPtr<IUserTransaction> opUserTransaction = IUserTransaction::Create(pDocument, GBMP_TR(L"创建类别"));
    
    //墙体类别
    OwnerPtr<ICategory> opCategoryWall = ICategory::Create(WALL_CATEGORY, GBMP_TR(L"墙体"), GBMP_TR(L"墙体"));
    ICategoryLibrary::Get(pDocument)->AddCategory(TransferOwnership(opCategoryWall));
    
    //墙体标注类别
    OwnerPtr<ICategory> opCategoryWallTag = ICategory::Create(WALLTAG_CATEGORY, GBMP_TR(L"墙体标注"), GBMP_TR(L"墙体标注"));
    ICategoryLibrary::Get(pDocument)->AddCategory(TransferOwnership(opCategoryWallTag));
    
    //多段线PolyLine2d类别
    OwnerPtr<ICategory> opCategoryPolyLine2d = ICategory::Create(POLYLINE2D_CATEGORY, GBMP_TR(L"PolyLine2d"), GBMP_TR(L"PolyLine2d"));
    ICategoryLibrary::Get(pDocument)->AddCategory(TransferOwnership(opCategoryPolyLine2d));
    
    InitializeGraphicsStyle(pDocument);
    //提交事务
    opUserTransaction->CommitAndResetHistory();
}

void CategoryManager::InitializeGraphicsStyle(gcmp::IDocument* pDocument)
{
    //图形样式管理器
    IGraphicsStyleManager *pGraphicsStyleManager = pDocument->GetGraphicsStyleManager();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGraphicsStyleManager, L"获取图形样式管理器失败", L"GDMP", L"2023-09-30");
    
    //设置墙体的样式数据 如颜色、线型等
    OwnerPtr<IGraphicsStyleData> opStyleDataWall = IGraphicsStyleData::Create();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opStyleDataWall, L"创建图形样式数据失败", L"GDMP", L"2023-11-09");
    opStyleDataWall->SetProjectionLineColor(Color::Cyan);
    opStyleDataWall->SetColor(Color::Cyan);
    pGraphicsStyleManager->CreateGraphicsStyleForCategory(WALL_CATEGORY, *opStyleDataWall);
    
    OwnerPtr<IGraphicsStyleData> opStyleDataWallTag = IGraphicsStyleData::Create();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opStyleDataWallTag, L"创建图形样式数据失败", L"GDMP", L"2023-11-09");
    opStyleDataWallTag->SetProjectionLineColor(Color::Black);
    opStyleDataWallTag->SetColor(Color::Black);
    pGraphicsStyleManager->CreateGraphicsStyleForCategory(WALLTAG_CATEGORY, *opStyleDataWallTag);
    
    OwnerPtr<IGraphicsStyleData> opStyleDataPolyLine2d = IGraphicsStyleData::Create();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opStyleDataPolyLine2d, L"创建图形样式数据失败", L"GDMP", L"2023-11-09");
    opStyleDataPolyLine2d->SetProjectionLineColor(Color::Black);
    opStyleDataPolyLine2d->SetColor(Color::Black);
    pGraphicsStyleManager->CreateGraphicsStyleForCategory(POLYLINE2D_CATEGORY, *opStyleDataPolyLine2d);
    
    IGraphicsStyle* pGraphicsStyle = IGraphicsStyle::Create(pDocument, gcmp::ElementCreationOptions::Normal);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGraphicsStyle, L"创建图形样式失败", L"GDMP", L"2023-11-09");
    OwnerPtr<IGraphicsStyleData> opGraphicsStyleData = IGraphicsStyleData::Create();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opGraphicsStyleData, L"创建图形样式数据失败", L"GDMP", L"2023-11-09");
    opGraphicsStyleData->SetColor(Color::Blue);
    opGraphicsStyleData->SetTransparency(0.5);
    pGraphicsStyle->SetGraphicsStyleData(*opGraphicsStyleData);
    
    IElementBasicInformation* pElementBasicInfo = pGraphicsStyle->GetBasicInformation();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementBasicInfo, L"ElementBasicInformation为空", L"GDMP", L"2023-11-09");
    pElementBasicInfo->SetName(WALL_STYLE_NAME);
}

void CategoryManager::InitializeGraphicsMaterial(gcmp::IDocument* pDocument)
{
    IGraphicsMaterial* pMaterial = IGraphicsMaterial::Create(pDocument, ElementCreationOptions::Normal, WALL_MATERIAL_NAME);
    if(pMaterial)
    {
        OwnerPtr<IGraphicsMaterialData> opMaterialData = pMaterial->GetGraphicsMaterialDataFromMaterialInstance();
        if(opMaterialData)
        {
            opMaterialData->SetDiffuse(Color(200, 200, 0));
            opMaterialData->SetAmbient(Color(100, 200, 0));
            opMaterialData->SetSpecular(Color(255, 255, 255));
            opMaterialData->SetShininess(1.0);
            opMaterialData->EnableDiffuseTexture(false);
    
            pMaterial->UpdateMaterialInstanceByGraphicsMaterialData(*(opMaterialData.get()));
        }
    }
}
